package rwt.server.core;

/**
 * A user account as stored in the database. Enables user authentication.
 */
public class User
{
	/**
	 * Enumeration of user roles: basic and manager. The role of a user affects the amount of features
	 * available on their main page.
	 */
	public enum UserRole
	{
		BASIC(0, "Basic"), MANAGER(1, "Manager");
		
		public static UserRole fromInt(int value)
		{
			for (UserRole type : UserRole.values())
			{
				if (type.getValue() == value)
					return type;
			}
			throw new RuntimeException("Value " + value + " out of range.");
		}
		
		public static UserRole fromName(String name)
		{
			for (UserRole type : UserRole.values())
			{
				if (type.getName().equals(name))
					return type;
			}
			throw new RuntimeException("Role '" + name + "' does not exist.");
		}
		
		private int value;
		private String name;
		
		private UserRole(int value, String name)
		{
			this.value = value;
			this.name = name;
		}
		
		public int getValue()
		{
			return value;
		}
		
		public String getName()
		{
			return name;
		}
	}
	
	public static final int INVALID_ID = 0;
	
	private int id;
	private String firstName;
	private String lastName;
	private String userName;
	private String passwordHash; // Password, MD5-hashed. Typically null, except for during user creation.
	private UserRole role;
	
	public User()
	{
	}
	
	public int getId()
	{
		return id;
	}
	
	public void setId(int id)
	{
		this.id = id;
	}
	
	public String getFirstName()
	{
		return firstName;
	}
	
	public void setFirstName(String firstName)
	{
		this.firstName = firstName;
	}
	
	public String getLastName()
	{
		return lastName;
	}
	
	public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}
	
	public String getUserName()
	{
		return userName;
	}
	
	public void setUserName(String userName)
	{
		this.userName = userName;
	}
	
	public String getPasswordHash()
	{
		return passwordHash;
	}
	
	public void setPasswordHash(String passwordHash)
	{
		this.passwordHash = passwordHash;
	}
	
	public UserRole getRole()
	{
		return role;
	}
	
	public void setRole(UserRole userType)
	{
		this.role = userType;
	}
	
	@Override
	public String toString()
	{
		return "WtUser [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", userName="
				+ userName + ", passwordHash=" + passwordHash + ", role=" + role.getName() + "]";
	}
	
	public String getFullName()
	{
		return (firstName + " " + lastName).trim();
	}
}
